import { CopyControllerBase } from "./CopyControllerBase";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { EatBeansIcon } from "../eatBeans/EatBeansIcon";
import { SEatBeansInfo } from "../../../../../Message/Public/SEatBeansInfo";
import { GameConst } from "../../../../component/gconst/GameConst";
import { ConstConfig } from "../../../resource/ConstConfig";
import { EatBeansBonusWin } from "../eatBeans/EatBeansBonusWin";
import { TCopy } from "../../../../../Message/Db/Tables/TCopy";
import { EatBeansSmallMap } from "../eatBeans/EatBeansSmallMap";
import { GameController } from "../../../mvc/GameController";
import { DataEvent } from "../../../events/DataEvent";
import { EatBeansDoingStatus } from "../eatBeans/EatBeansDoingStatus";
import { SceneRange } from "../../../scene/map/SceneRange";
import { SystemSetter } from "../../system/SystemSetter";
import { ThingUtil } from "../../../scene/layer/utils/ThingUtil";
import { EatBeansInfoPanel } from "../eatBeans/EatBeansInfoPanel";
import { RolePlayer } from "../../../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../../../scene/events/PlayerEvent";
import { EatBeansReliveWin } from "../eatBeans/EatBeansReliveWin";
import { CopyConfig } from "../../../resource/CopyConfig";
import { MsgManager } from "../../../manager/MsgManager";
import { Language } from "../../../../../extend/language/Language";
import { CopyUtil } from "../CopyUtil";
import { Global } from "../../../../../com/gengine/global/Global";
import { EatBeansEnterWin } from "../eatBeans/EatBeansEnterWin";
//class EatBeansController
    
    export  class EatBeansController extends CopyControllerBase
    {
        constructor()
        {
            super();
            return;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.EatBeansOpen, this.copyOpenHandler);
            NetDispatcher.addCmdListener(ServerCommand.EatBeansClose, this.copyCloseloseHandler);
            NetDispatcher.addCmdListener(ServerCommand.EatBeansInfoUpdate, this.copyUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.EatBeansRoleInfo, this.copyRoleInfoHandler);
            NetDispatcher.addCmdListener(ServerCommand.EatBeansSmoke, this.smokeHandler);
            NetDispatcher.addCmdListener(ServerCommand.EatBeansResult, this.resultHandler);
            NetDispatcher.addCmdListener(ServerCommand.EatBeansStart, this.startHandler);
            Dispatcher.addEventListener(EventName.EatBeansAskEnter, this.askEnterHandler);
            Dispatcher.addEventListener(EventName.ShowHideEatBeansSmallMap, this.showHideSmallMapHandler);
            NetDispatcher.addCmdListener(ServerCommand.Copy_CopyNumInfoChange, this.copyNumInfoChangeHandler);
            return;
        }

        /* internal  */copyOpenHandler(arg1: Object): void
        {
            if (this._icon == null) 
            {
                this._icon = new EatBeansIcon();
            }
            this._icon.show();
            this._icon.showAchievementEffect(true);
            this.checkAndHideIconEffect();
            return;
        }

        /* internal  */copyCloseloseHandler(arg1: Object): void
        {
            if (this._icon != null) 
            {
                this._icon.hide();
            }
            return;
        }

        /* internal  */copyUpdateHandler(arg1: Object): void
        {
            var loc1=arg1 as SEatBeansInfo;
            if (this._infoPanel != null) 
            {
                this._infoPanel.updateInfo(loc1);
            }
            return;
        }

        /* internal  */copyRoleInfoHandler(arg1: Object): void
        {
            if (cache.eatBeans.roleDic == null) 
            {
                return;
            }
            if (this._smallMap != null) 
            {
                this._smallMap.updateRoleInfo(cache.eatBeans.roleDic);
            }
            return;
        }

        /* internal  */startHandler(arg1: Object): void
        {
            if (this._infoPanel != null) 
            {
                this._infoPanel.searchingStatus = false;
                this._infoPanel.setLeftTime(GameConst.EatBeansCopyTime);
            }
            if (this._smallMap) 
            {
                this._allPlayers = this._smallMap.allPlayers;
            }
            return;
        }

        /* internal  */smokeHandler(arg1: Object): void
        {
            if (this._smallMap == null) 
            {
                return;
            }
            var loc1=ConstConfig.instance.getValueByName("EatBeansMiasmaTime");
            if (loc1 == 0) 
            {
                loc1 = 20;
            }
            this._smallMap.setSmoke(loc1);
            return;
        }

        /* internal  */resultHandler(arg1: Object): void
        {
            if (!cache.copy.isInEatBeansCopy()) 
            {
                return;
            }
            if (this._bonusWin == null) 
            {
                this._bonusWin = new EatBeansBonusWin();
            }
            this._bonusWin.updateData(cache.eatBeans.getResThiefs(), cache.eatBeans.getResDefenders());
            this._bonusWin.show();
            if (!(this._reliveWin == null) && !this._reliveWin.isHide) 
            {
                this._reliveWin.hide();
            }
            if (this._infoPanel) 
            {
                this._infoPanel.setEnd();
            }
            return;
        }

        public /* override */ enterCopy(arg1: TCopy): void
        {
            super.enterCopy(arg1);
            if (this._smallMap == null) 
            {
                this._smallMap = new EatBeansSmallMap();
            }
            this._smallMap.show();
            GameController.gameui.getGameui().showAllUI(false);
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShowOrHideShortcut, false));
            this.showHideInfoPanel(true);
            this._infoPanel.reInit();
            this._infoPanel.searchingStatus = true;
            cache.eatBeans.doingStatus = EatBeansDoingStatus.StandBy;
            if (!(this._enterWin == null) && !this._enterWin.isHide) 
            {
                this._enterWin.hide();
            }
            SceneRange.cancelNoMoveWH(true);
            this.setShowAllMode();
            return;
        }

        /* internal  */setShowAllMode(arg1: Boolean=false): void
        {
            if (arg1) 
            {
                SystemSetter.localSetter.hideNearPlayerPet = this._saveHideNear;
                SystemSetter.localSetter.hideModel = this._saveHideMode;
            }
            else 
            {
                this._saveHideNear = SystemSetter.localSetter.hideNearPlayerPet;
                this._saveHideMode = SystemSetter.localSetter.hideModel;
                SystemSetter.localSetter.hideNearPlayerPet = false;
                SystemSetter.localSetter.hideModel = false;
            }
            ThingUtil.isEntitySort = true;
            Dispatcher.dispatchEvent(new DataEvent(EventName.ScreenOrShowPlayer, SystemSetter.currentSetter.hideNearPlayerPet));
            Dispatcher.dispatchEvent(new DataEvent(EventName.SaveForSliderChange));
            return;
        }

        /* internal  */showHideInfoPanel(arg1: Boolean): void
        {
            if (arg1) 
            {
                if (this._infoPanel == null) 
                {
                    this._infoPanel = new EatBeansInfoPanel();
                }
                this._infoPanel.show();
                this._infoPanel.updatePosition();
            }
            else if (this._infoPanel != null) 
            {
                this._infoPanel.hide();
            }
            return;
        }

        public /* override */ leaveCopy(): void
        {
            super.leaveCopy();
            this._smallMap.hide();
            this.showHideInfoPanel(false);
            GameController.gameui.getGameui().showAllUI(true);
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, true));
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShowOrHideShortcut, true));
            cache.eatBeans.clearRole();
            SceneRange.cancelNoMoveWH(false);
            this.setShowAllMode(true);
            if (this._reliveWin && !this._reliveWin.isHide) 
            {
                this._reliveWin.hide();
            }
            this.showAllPlayersInfo();
            return;
        }

        /* internal  */showAllPlayersInfo(): void
        {
            var loc1=null;
            if (this._allPlayers == null || this._allPlayers.length == 0) 
            {
                return;
            }
            var loc2=0;
            var loc3=this._allPlayers;
            for(loc1 of loc3) 
            {
                loc1.titleVisible = true;
                loc1.fabaoVisible = true;
            }
            return;
        }

        protected /* override */ addListener(): void
        {
            super.addListener();
            RolePlayer.instance.addEventListener(PlayerEvent.ENTITY_DEAD, this.onRoleDead, false, 0, true);
            return;
        }

        protected /* override */ removeListener(): void
        {
            super.removeListener();
            RolePlayer.instance.removeEventListener(PlayerEvent.ENTITY_DEAD, this.onRoleDead);
            RolePlayer.instance.removeEventListener(PlayerEvent.ENTITY_Relived, this.onRelived);
            return;
        }

        /* internal  */onRoleDead(arg1: PlayerEvent): void
        {
            RolePlayer.instance.addEventListener(PlayerEvent.ENTITY_Relived, this.onRelived, false, 0, true);
            if (!cache.copy.isInEatBeansCopy()) 
            {
                return;
            }
            if (this._reliveWin == null) 
            {
                this._reliveWin = new EatBeansReliveWin();
            }
            if (cache.eatBeans.getRoleID(cache.role.entityInfo.entityId) != EatBeansSmallMap.ThiefID) 
            {
                if (cache.eatBeans.getRoleID(cache.role.entityInfo.entityId) == EatBeansSmallMap.DefenderID) 
                {
                    this._reliveWin.setShowMode(EatBeansReliveWin.ReliveModeDefender);
                }
            }
            else 
            {
                if (!(this._smallMap == null) && this._smallMap.getFriendNum() == 0) 
                {
                    if (!(this._reliveWin == null) && !this._reliveWin.isHide) 
                    {
                        this._reliveWin.hide();
                    }
                    return;
                }
                this._reliveWin.setShowMode(EatBeansReliveWin.ReliveModeThief);
            }
            this._reliveWin.show();
            return;
        }

        /* internal  */onRelived(arg1: PlayerEvent): void
        {
            RolePlayer.instance.removeEventListener(PlayerEvent.ENTITY_Relived, this.onRelived);
            if (this._reliveWin && !this._reliveWin.hide()) 
            {
                this._reliveWin.hide();
            }
            return;
        }

        /* internal  */showHideSmallMapHandler(arg1: DataEvent): void
        {
            if (!(this._smallMap == null) && !this._smallMap.isHide) 
            {
                this._smallMap.hide();
            }
            else 
            {
                if (this._smallMap == null) 
                {
                    this._smallMap = new EatBeansSmallMap();
                }
                this._smallMap.show();
            }
            return;
        }

        /* internal  */askEnterHandler(arg1: DataEvent): void
        {
            var loc7=null;
            var loc8=null;
            var loc1=CopyConfig.instance.getInfoByCode(168);
            var loc2=60;
            var loc3=120;
            if (loc1 != null) 
            {
                loc2 = loc1.enterMinLevel;
                loc3 = loc1.enterMaxLevel;
            }
            if (cache.role.roleInfo.level < loc2 || cache.role.roleInfo.level >= loc3) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80362));
                return;
            }
            var loc4=5;
            if (loc1 != null) 
            {
                loc4 = CopyUtil.getCopyNumByDay(loc1);
            }
            if (cache.copy.getHasEnterNumById(168) >= loc4 && !Global.isDebugModle) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80363));
                return;
            }
            if (this._enterWin == null) 
            {
                this._enterWin = new EatBeansEnterWin();
                if ((loc7 = CopyConfig.instance.getInfoByCode(168)) != null) 
                {
                    loc8 = loc7.reward.split(",");
                }
                if (!(loc8 == null) && loc8.length > 0) 
                {
                    this._enterWin.initBonuse(loc8);
                }
            }
            var loc5=3;
            var loc6;
            if ((loc6 = cache.copy.getCopyNumInfoByCode(168)) != null) 
            {
                loc5 = loc6.todayEnterNum;
            }
            this._enterWin.updateTodayTimes(loc5, loc4);
            this._enterWin.show();
            return;
        }

        /* internal  */copyNumInfoChangeHandler(arg1: Object): void
        {
            this.checkAndHideIconEffect();
            return;
        }

        /* internal  */checkAndHideIconEffect(): void
        {
            if (this._icon == null || this._icon.isHide) 
            {
                return;
            }
            var loc1=5;
            var loc2=CopyConfig.instance.getInfoByCode(168);
            if (loc2 != null) 
            {
                loc1 = CopyUtil.getCopyNumByDay(loc2);
            }
            var loc3=3;
            var loc4;
            if ((loc4 = cache.copy.getCopyNumInfoByCode(168)) != null) 
            {
                loc3 = loc4.todayEnterNum;
            }
            if (loc3 >= loc1) 
            {
                this._icon.showAchievementEffect(false);
            }
            return;
        }

        /* internal */ /* var */_icon: EatBeansIcon;

        /* internal */ /* var */_smallMap: EatBeansSmallMap;

        /* internal */ /* var */_infoPanel: EatBeansInfoPanel;

        /* internal */ /* var */_enterWin: EatBeansEnterWin;

        /* internal */ /* var */_bonusWin: EatBeansBonusWin;

        /* internal */ /* var */_reliveWin: EatBeansReliveWin;

        /* internal */ /* var */_saveHideNear: Boolean;

        /* internal */ /* var */_saveHideMode: Boolean;

        /* internal */ /* var */_allPlayers: Array<any>;
    }
